package avicit.bdp.dcs.rule.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.dcs.job.dto.JobDTO;
import avicit.bdp.dcs.job.service.DcsJobService;
import avicit.bdp.dcs.rule.dao.TableRuleDao;
import avicit.bdp.dcs.rule.dto.TableRuleDTO;
import avicit.bdp.dcs.tools.enums.RuleType;
import avicit.bdp.dcs.utils.DcsErrorCode;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.core.exception.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间：2020-08-06 14:03
 * @类说明：
 * @修改记录：
 */
@Service
@Transactional
public class TableRuleService extends BaseService<TableRuleDao, TableRuleDTO> {

  @Autowired private DcsJobService jobService;

  public String saveTableRule(TableRuleDTO tableRuleDTO) throws Exception {
    JobDTO jobDTO = jobService.selectByPrimaryKey(tableRuleDTO.getJobId());
    if (jobDTO == null) {
      throw new BusinessException(
          String.format(DcsErrorCode.JOB_NULL.getDesc(), tableRuleDTO.getJobId()));
    }

    tableRuleDTO.setId(ComUtil.getId());
    tableRuleDTO.setName("修改规则");
    this.insert(tableRuleDTO);
    BdpLogUtil.log4Insert(tableRuleDTO);
    return this.buildTableRuleName(tableRuleDTO);
  }

  public String deleteTableRuleById(String tableRuleId) throws Exception {
    this.deleteByPrimaryKey(tableRuleId);
    return null;
  }

  public List<TableRuleDTO> getTableRuleListByJobId(String jobId) throws Exception {
    JobDTO jobDTO = jobService.selectByPrimaryKey(jobId);
    if (jobDTO == null) {
      throw new BusinessException(String.format(DcsErrorCode.JOB_NULL.getDesc(), jobId));
    }

    TableRuleDTO tableRuleDTOQuery = new TableRuleDTO();
    tableRuleDTOQuery.setJobId(jobId);
    List<TableRuleDTO> tableRuleList = this.selectList(tableRuleDTOQuery);
    if (CollectionUtils.isNotEmpty(tableRuleList)) {
      for (TableRuleDTO tableRule : tableRuleList) {
        tableRule.setName(this.buildTableRuleName(tableRule));
      }
    }
    return tableRuleList;
  }

  /**
   * 构建规则名称
   *
   * @param tableRuleDTO
   * @return
   */
  private String buildTableRuleName(TableRuleDTO tableRuleDTO) {
    StringBuilder builder = new StringBuilder();
    builder
        .append(tableRuleDTO.getName())
        .append(":表名(")
        .append(tableRuleDTO.getTableName())
        .append(")");

    if (RuleType.PREFIX_SUFFIX.getCode() == tableRuleDTO.getRuleType()) {
      builder
          .append("添加前缀:")
          .append(tableRuleDTO.getPrefix())
          .append(",后缀:")
          .append(tableRuleDTO.getSuffix());
    } else if (RuleType.RENAME.getCode() == tableRuleDTO.getRuleType()) {
      builder.append("更名:").append(tableRuleDTO.getNewName());
    } else {
      // 不支持
    }
    return builder.toString();
  }
}
